import { createRouter, createWebHistory } from 'vue-router'
import UserLogin from '../views/UserLogin.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: UserLogin
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('../views/Home.vue'),
      redirect: '/home/bounty',
      children: [
        {
          path: 'bounty',
          name: 'Bounty',//悬赏
          component: () => import('../views/modules/Bounty.vue')
        },
        {
          path: 'message',
          name: 'Message',//消息
          component: () => import('../views/modules/Message.vue')
        },
        // {
        //   path: 'add-bounty',
        //   name: 'AddBounty',//发布悬赏
        //   component: () => import('../views/modules/AddBounty.vue')
        // },
        // {
        //   path: 'cart',
        //   name: 'Cart',//购物车
        //   component: () => import('../views/modules/Cart.vue')
        // }
      ]
    },
    // {
    //   path: '/user-profile',
    //   name: 'UserProfile',//用户信息
    //   component: () => import('../views/UserProfile.vue')
    // }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== '/' && !token) {
    next('/')
  } else {
    next()
  }
})

export default router